package www.wen.com.common.lib.extension

import android.app.Activity
import android.content.Intent
import android.content.Intent.FLAG_ACTIVITY_SINGLE_TOP
import android.os.Bundle
import android.view.View
import androidx.annotation.ColorRes
import androidx.annotation.IdRes
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.navigation.Navigation.findNavController
import com.gyf.immersionbar.ImmersionBar
import www.wen.com.common.R

/**
 * @author WEN
 * @Description:
 * @date 2020/12/5 17:24
 */

fun Activity.initImmersion(
    isEnable: Boolean = true,
    @ColorRes statusBarColor: Int = R.color.colorFFFFFF,
    autoDarkModeAlpha: Float = 0.2f,
    fits: Boolean = false,
    keyboardEnable: Boolean = false
) {

    ImmersionBar
        .with(this)
        .fitsSystemWindows(fits)
        .statusBarColor(statusBarColor) //这里设置状态栏颜色默认白色,透明可设置Color.TRANSPARENT
        .autoDarkModeEnable(isEnable, autoDarkModeAlpha) // 自动根据StatusBar颜色调整深色模式与亮色模式，默认false
        .keyboardEnable(keyboardEnable)
        .init() //初始化
}

fun Activity.startActivityAgency(cls: Class<*>,isFinishAfterStart:Boolean = false) {
    if(isFinishAfterStart) finish()
    startActivity(Intent(this, cls))
}

fun Activity.startActivityLaunch(cls: Class<*>,flag:Int = Intent.FLAG_ACTIVITY_CLEAR_TOP,model:Int = FLAG_ACTIVITY_SINGLE_TOP) {
    startActivity(Intent(this, cls).apply {
        flags = flag
        addFlags(model)
    })
}

fun Activity.navigateNavHostAgency(view: View, @IdRes resId: Int, args: Bundle? = null) {
    findNavController(this, view.id)
        .navigate(resId,args)
}

fun FragmentActivity.isExitNavFragment(clazz: Class<*>,isExit:((Fragment)->Unit)? = null,isNotExit:(()->Unit)? = null):Boolean {
    val fragments: List<Fragment> = supportFragmentManager.fragments
    if (fragments.isNotEmpty()) {
        if (fragments[0] is www.wen.com.common.lib.fix.NavHostFragment) {
            val navHostFragment: www.wen.com.common.lib.fix.NavHostFragment = fragments[0] as www.wen.com.common.lib.fix.NavHostFragment
            val childFragments: List<Fragment> = navHostFragment.childFragmentManager.fragments
            if (childFragments.isNotEmpty()) {
                for (j in childFragments.indices) {
                    val fragment: Fragment = childFragments[j]
                    if (fragment.javaClass.isAssignableFrom(clazz)) {
                        isExit?.invoke(fragment)
                        return true
                    }
                }
            }
        }
    }
    isNotExit?.invoke()
    return false
}



fun Fragment.isExitFragment(clazz: Class<*>,isExit:((Fragment)->Unit)? = null,isNotExit:(()->Unit)? = null):Boolean {
    val fragments: List<Fragment> = childFragmentManager.fragments
    if (fragments.isNotEmpty()) {
        if (fragments[0] is www.wen.com.common.lib.fix.NavHostFragment) {
            val navHostFragment: www.wen.com.common.lib.fix.NavHostFragment = fragments[0] as www.wen.com.common.lib.fix.NavHostFragment
            val childFragments: List<Fragment> = navHostFragment.childFragmentManager.fragments
            if (childFragments.isNotEmpty()) {
                for (j in childFragments.indices) {
                    val fragment: Fragment = childFragments[j]
                    if (fragment.javaClass.isAssignableFrom(clazz)) {
                        isExit?.invoke(fragment)
                        return true
                    }
                }
            }
        }
    }
    isNotExit?.invoke()
    return false
}

fun Activity.startDestination(navigationId:Int, startDestId:Int, containerId: Int = R.id.container, bundle:(()->Bundle)? = null){
    findNavController(this,containerId).apply {
        setGraph(navInflater.inflate(navigationId).apply {
            startDestination = startDestId
        }, bundle?.invoke()?: Bundle())
    }
}

